home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sfilter2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.8 KB  |  266 lines

  1. /* Copyright (C) 1991, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sfilter2.c,v 1.3 2000/09/19 19:00:49 lpd Exp $ */
  20. /* Simple Level 2 filters */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "memory_.h"
  23. #include "gdebug.h"
  24. #include "strimpl.h"
  25. #include "sa85x.h"
  26. #include "sbtx.h"
  27. #include "scanchar.h"
  28.  
  29. /* ------ ASCII85Encode ------ */
  30.  
  31. private_st_A85E_state();
  32.  
  33. /* Initialize the state */
  34. private int
  35. s_A85E_init(stream_state * st)
  36. {
  37.     stream_A85E_state *const ss = (stream_A85E_state *) st;
  38.  
  39.     return s_A85E_init_inline(ss);
  40. }
  41.  
  42. /* Process a buffer */
  43. #define LINE_LIMIT 79        /* not 80, to satisfy Genoa FTS */
  44. private int
  45. s_A85E_process(stream_state * st, stream_cursor_read * pr,
  46.            stream_cursor_write * pw, bool last)
  47. {
  48.     stream_A85E_state *const ss = (stream_A85E_state *) st;
  49.     register const byte *p = pr->ptr;
  50.     register byte *q = pw->ptr;
  51.     byte *qn = q + (LINE_LIMIT - ss->count); /* value of q before next EOL */
  52.     const byte *rlimit = pr->limit;
  53.     byte *wlimit = pw->limit;
  54.     int status = 0;
  55.     int prev = ss->last_char;
  56.     int count;
  57.  
  58.     if_debug3('w', "[w85]initial ss->count = %d, rcount = %d, wcount = %d\n",
  59.           ss->count, (int)(rlimit - p), (int)(wlimit - q));
  60.     for (; (count = rlimit - p) >= 4; p += 4) {
  61.     ulong word =
  62.         ((ulong) (((uint) p[1] << 8) + p[2]) << 16) +
  63.         (((uint) p[3] << 8) + p[4]);
  64.  
  65.     if (word == 0) {
  66.         if (q >= qn) {
  67.         if (wlimit - q < 2) {
  68.             status = 1;
  69.             break;
  70.         }
  71.         *++q = prev = '\n';
  72.         qn = q + LINE_LIMIT;
  73.         if_debug1('w', "[w85]EOL at %d bytes written\n",
  74.               (int)(q - pw->ptr));
  75.         } else {
  76.         if (q >= wlimit) {
  77.             status = 1;
  78.             break;
  79.         }
  80.         }
  81.         *++q = prev = 'z';
  82.     } else {
  83.         ulong v4 = word / 85;    /* max 85^4 */
  84.         ulong v3 = v4 / 85;    /* max 85^3 */
  85.         uint v2 = v3 / 85;    /* max 85^2 */
  86.         uint v1 = v2 / 85;    /* max 85 */
  87.  
  88. put:        if (q + 5 > qn) {
  89.         if (q >= wlimit) {
  90.             status = 1;
  91.             break;
  92.         }
  93.         *++q = prev = '\n';
  94.         qn = q + LINE_LIMIT;
  95.         if_debug1('w', "[w85]EOL at %d bytes written\n",
  96.               (int)(q - pw->ptr));
  97.         goto put;
  98.         }
  99.         if (wlimit - q < 5) {
  100.         status = 1;
  101.         break;
  102.         }
  103.         q[1] = (byte) v1 + '!';
  104.         q[2] = (byte) (v2 - v1 * 85) + '!';
  105.         q[3] = (byte) ((uint) v3 - v2 * 85) + '!';
  106.         q[4] = (byte) ((uint) v4 - (uint) v3 * 85) + '!';
  107.         q[5] = (byte) ((uint) word - (uint) v4 * 85) + '!';
  108.         /*
  109.          * '%%' or '%!' at the beginning of the line will confuse some
  110.          * document managers: insert (an) EOL(s) if necessary to prevent
  111.          * this.
  112.          */
  113.         if (q[1] == '%') {
  114.         if (prev == '%') {
  115.             if (qn - q == LINE_LIMIT - 1) {
  116.             /* A line would begin with %%. */
  117.             *++q = prev = '\n';
  118.             qn = q + LINE_LIMIT;
  119.             if_debug1('w',
  120.                   "[w85]EOL for %%%% at %d bytes written\n",
  121.                   (int)(q - pw->ptr));
  122.             goto put;
  123.             }
  124.         } else if (prev == '\n' && (q[2] == '%' || q[2] == '!')) {
  125.             /*
  126.              * We may have to insert more than one EOL if
  127.              * there are more than two %s in a row.
  128.              */
  129.             int extra =
  130.             (q[2] == '!' ? 1 : /* else q[2] == '%' */
  131.              q[3] == '!' ? 2 :
  132.              q[3] != '%' ? 1 :
  133.              q[4] == '!' ? 3 :
  134.              q[4] != '%' ? 2 :
  135.              q[5] == '!' ? 4 :
  136.              q[5] != '%' ? 3 : 4);
  137.  
  138.             if (wlimit - q < 5 + extra) {
  139.             status = 1;
  140.             break;
  141.             }
  142.             if_debug6('w', "[w]%c%c%c%c%c extra = %d\n",
  143.                   q[1], q[2], q[3], q[4], q[5], extra);
  144.             switch (extra) {
  145.             case 4:
  146.                 q[9] = q[5], q[8] = '\n';
  147.                 goto e3;
  148.             case 3:
  149.                 q[8] = q[5];
  150.               e3:q[7] = q[4], q[6] = '\n';
  151.                 goto e2;
  152.             case 2:
  153.                 q[7] = q[5], q[6] = q[4];
  154.               e2:q[5] = q[3], q[4] = '\n';
  155.                 goto e1;
  156.             case 1:
  157.                 q[6] = q[5], q[5] = q[4], q[4] = q[3];
  158.               e1:q[3] = q[2], q[2] = '\n';
  159.             }
  160.             if_debug1('w', "[w85]EOL at %d bytes written\n",
  161.                   (int)(q + 2 * extra - pw->ptr));
  162.             qn = q + 2 * extra + LINE_LIMIT;
  163.             q += extra;
  164.         }
  165.         } else if (q[1] == '!' && prev == '%' &&
  166.                qn - q == LINE_LIMIT - 1
  167.                ) {
  168.         /* A line would begin with %!. */
  169.         *++q = prev = '\n';
  170.         qn = q + LINE_LIMIT;
  171.         if_debug1('w', "[w85]EOL for %%! at %d bytes written\n",
  172.               (int)(q - pw->ptr));
  173.         goto put;
  174.         }
  175.         prev = *(q += 5);
  176.     }
  177.     }
  178.  end:
  179.     ss->count = LINE_LIMIT - (qn - q);
  180.     /* Check for final partial word. */
  181.     if (last && status == 0 && count < 4) {
  182.     int nchars = (count == 0 ? 2 : count + 3);
  183.  
  184.     if (wlimit - q < nchars)
  185.         status = 1;
  186.     else if (q + nchars > qn) {
  187.         *++q = '\n';
  188.         qn = q + LINE_LIMIT;
  189.         goto end;
  190.     }
  191.     else {
  192.         ulong word = 0;
  193.         ulong divisor = 85L * 85 * 85 * 85;
  194.  
  195.         switch (count) {
  196.         case 3:
  197.             word += (uint) p[3] << 8;
  198.         case 2:
  199.             word += (ulong) p[2] << 16;
  200.         case 1:
  201.             word += (ulong) p[1] << 24;
  202.             p += count;
  203.             while (count-- >= 0) {
  204.             ulong v = word / divisor;  /* actually only a byte */
  205.  
  206.             *++q = (byte) v + '!';
  207.             word -= v * divisor;
  208.             divisor /= 85;
  209.             }
  210.             /*case 0: */
  211.         }
  212.         *++q = '~';
  213.         *++q = '>';
  214.     }
  215.     }
  216.     if_debug3('w', "[w85]final ss->count = %d, %d bytes read, %d written\n",
  217.           ss->count, (int)(p - pr->ptr), (int)(q - pw->ptr));
  218.     pr->ptr = p;
  219.     if (q > pw->ptr)
  220.     ss->last_char = *q;
  221.     pw->ptr = q;
  222.     return status;
  223. }
  224. #undef LINE_LIMIT
  225.  
  226. /* Stream template */
  227. const stream_template s_A85E_template = {
  228.     &st_A85E_state, s_A85E_init, s_A85E_process, 4, 6
  229. };
  230.  
  231. /* ------ ByteTranslateEncode/Decode ------ */
  232.  
  233. private_st_BT_state();
  234.  
  235. /* Process a buffer.  Note that the same code serves for both streams. */
  236. private int
  237. s_BT_process(stream_state * st, stream_cursor_read * pr,
  238.          stream_cursor_write * pw, bool last)
  239. {
  240.     stream_BT_state *const ss = (stream_BT_state *) st;
  241.     const byte *p = pr->ptr;
  242.     byte *q = pw->ptr;
  243.     uint rcount = pr->limit - p;
  244.     uint wcount = pw->limit - q;
  245.     uint count;
  246.     int status;
  247.  
  248.     if (rcount <= wcount)
  249.     count = rcount, status = 0;
  250.     else
  251.     count = wcount, status = 1;
  252.     while (count--)
  253.     *++q = ss->table[*++p];
  254.     pr->ptr = p;
  255.     pw->ptr = q;
  256.     return status;
  257. }
  258.  
  259. /* Stream template */
  260. const stream_template s_BTE_template = {
  261.     &st_BT_state, NULL, s_BT_process, 1, 1
  262. };
  263. const stream_template s_BTD_template = {
  264.     &st_BT_state, NULL, s_BT_process, 1, 1
  265. };
  266.